package com.ruoyi.system.repository.service.impl;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.ruoyi.system.repository.domain.Milktea;
import com.ruoyi.system.repository.mapper.MilkteaMapper;
import com.ruoyi.system.repository.service.IMilkteaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 奶茶Service业务层处理
 *
 * @author qiang
 * @date 2020-12-24
 */
@Service
public class MilkteaServiceImpl implements IMilkteaService {
    @Autowired
    private MilkteaMapper milkteaMapper;

    /**
     * 查询奶茶
     *
     * @param id 奶茶ID
     * @return 奶茶
     */
    @Override
    public Milktea selectMilkteaById(String id) {
        return milkteaMapper.selectMilkteaById(id);
    }

    /**
     * 查询奶茶列表
     *
     * @param milktea 奶茶
     * @return 奶茶
     */
    @Override
    public List<Milktea> selectMilkteaList(Milktea milktea) {
        return milkteaMapper.selectMilkteaList(milktea);
    }

    /**
     * 新增奶茶
     *
     * @param milktea 奶茶
     * @return 结果
     */
    @Override
    public int insertMilktea(Milktea milktea) {
        return milkteaMapper.insertMilktea(milktea);
    }

    /**
     * 修改奶茶
     *
     * @param milktea 奶茶
     * @return 结果
     */
    @Override
    public int updateMilktea(Milktea milktea) {
        return milkteaMapper.updateMilktea(milktea);
    }

    /**
     * 批量删除奶茶
     *
     * @param ids 需要删除的奶茶ID
     * @return 结果
     */
    @Override
    public int deleteMilkteaByIds(String[] ids) {
        return milkteaMapper.deleteMilkteaByIds(ids);
    }

    /**
     * 删除奶茶信息
     *
     * @param id 奶茶ID
     * @return 结果
     */
    @Override
    public int deleteMilkteaById(String id) {
        return milkteaMapper.deleteMilkteaById(id);
    }

    @Override
    public List<Milktea> recommend(){
        //随机选择8款奶茶向用户推荐
        int nums=8;
        int start=1;
        int end=countMilktea();
        List<Integer> list = new ArrayList();
        List<Milktea> ret = new ArrayList();
        Random r = new Random();
        while(list.size()!=nums){
            //生成指定范围随机数
            int num=r.nextInt(end-start)+start;
            if(!list.contains(num)){
                list.add(num);
            }
        }

        for(Object l:list){
            NumberFormat formatter=NumberFormat.getNumberInstance();
            formatter.setMinimumIntegerDigits(4);//设置整数部分的最小位数
            formatter.setGroupingUsed(false);
            String s=formatter.format(Integer.parseInt(l.toString()));

            System.out.println(s);
            ret.add(selectMilkteaById(s));
        }

        return ret;
    }


    @Override
    public List<Milktea> selectByType(String type){
        return milkteaMapper.selectByType(type);
    }

    @Override
    public int countMilktea(){
        return milkteaMapper.countMilktea();
    }

    @Override
    public int countType(){
        return milkteaMapper.countType();
    }

    @Override
    public List<Milktea> selectAllMilktea(){
        return milkteaMapper.selectAll();
    }

    @Override
    public Milktea selectOneMilktea(String id){
        return milkteaMapper.selectMilkteaById(id);
    }

}
